package com.fanfanwang.ffw.app.asynctask;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.bean.SucLoginBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.fragment.MainTingcheFragment;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;
import com.fanfanwang.ffw.app.ui.login.LoginAct;
import com.fanfanwang.ffw.app.ui.login.RegisterAct;

public class LoginAsyncTask extends IssAsyncTask<String, String, SucLoginBean> {
	private Activity mActivity;
	private String mobile;
	private String password;

	public LoginAsyncTask(Activity activity, String mobile, String password) {
		super(activity);
		mActivity = activity;
		this.mobile = mobile;
		this.password = password;
	}

	@Override
	protected SucLoginBean doInBackground(String... params) {
		// TODO addAuctionPrice($apiusername,$apipassword,$userid,$pid,$price)
		SucLoginBean sucLoginBean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("username", mobile);
		paramsMap.put("password", Md5Util.getMD5To32(password));
		try {
			sucLoginBean = IssNetLib.getInstance(mActivity).getLogin(
					WebServiceHelper._LOGIN, paramsMap);
			// int i=0;
		} catch (HttpRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_net_error);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_json);
		}
		return sucLoginBean;
	}

	@Override
	protected void onPostExecute(SucLoginBean result) {
		super.onPostExecute(result);
		if (exception != null) {
			ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {
				if (result.data != null) {
					if(LoginAct.iGetLoginDate!=null)
					{
						LoginAct.iGetLoginDate.setLoginData(result.data);
					}
					if(RegisterAct.iRegable!=null)
					{
						RegisterAct.iRegable.setLoginSuccess(result.data);
					}
				}
			} else if (result.status == 0) {
				if (!TextUtils.isEmpty(result.msg)) {
					try {
						ToastAlone.showToast(mActivity,
								URLDecoder.decode(result.msg, "utf-8"),
								ToastAlone.LENGTH_SHORT);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					if (!TextUtils.isEmpty(result.msg)) {
						ToastAlone.showToast(mActivity,
								AppConstant.URLDecoderString(result.msg),
								Toast.LENGTH_SHORT).show();
					} else {
						ToastAlone.showToast(
								mActivity,
								mActivity.getResources().getString(
										R.string.e_net_error)).show();
					}
				}

			}
		}
	}
}
